package com.menhai.personmanagement.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.FollowupRecordCondition;
import com.menhai.personmanagement.entity.MbHealthPlanAdvice;
import com.menhai.personmanagement.entity.MbHealthPlanModel;
import com.menhai.personmanagement.mapper.MbHealthPlanAdviceMapper;
import com.menhai.personmanagement.mapper.MbHealthPlanModelMapper;
/**
 * <p>
 * 健康计划表 服务类
 * </p>
 *
 * @author shikun
 * @since 2021-11-25
 */
 @Service
public class MbHealthPlanService{

   @Autowired
   private MbHealthPlanModelMapper bHealthPlanModelMapper;
   @Autowired
   private MbHealthPlanAdviceMapper mbHealthPlanAdviceMapper;
   
   
   /**
    * 健康计划显示患者
    * @param patientName
    * @param newaddress
    * @return
    */
   public IPage<List<Map<String, Object>>> getListByCondition(String patientName,String newaddress,String hosnum,MbHealthPlanAdvice mbHealthPlanAdvice){
	 Page<MbHealthPlanAdvice> page = new Page<>(mbHealthPlanAdvice.getPageIndex(),mbHealthPlanAdvice.getPageSize());
	 String[] hoStrings=hosnum.split(",");
	 return bHealthPlanModelMapper.getListByCondition(page,patientName, newaddress,hoStrings);
//	 return bHealthPlanModelMapper.getListByCondition(page,patientName, newaddress,hosnum);
   }
   
   
   
   	/**
   	 * 新建健康建议模板
   	 * @param mbHealthPlanModel
   	 * @return
   	 */
 	@Transactional
 	public Map<String, Object> addMbHealthAdviceModel(MbHealthPlanModel mbHealthPlanModel) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			Integer iRet = bHealthPlanModelMapper.insert(mbHealthPlanModel);		
 			if(iRet == 0){  //没有插入成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "插入成功的条数为0，新建健康建议模板失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "新建健康建议模板成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "新建健康建议模板卡失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
 	
 	/**
 	 * 查询所有模板数据
 	 * @param hosnum
 	 * @return
 	 */
 	public List<MbHealthPlanModel> getAdviceModelListByCondition(String hosnum,String adviceKind){
 		return bHealthPlanModelMapper.getAdviceModelListByCondition(hosnum,adviceKind);
 	}
   
 	
 	/**
 	 * 删除建议模板
 	 * @param modelId
 	 * @return
 	 */
 	@Transactional
 	public Map<String, Object> deleteAdviceModel(String modelId) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			Integer iRet = bHealthPlanModelMapper.deleteAdviceModel(modelId);	
 			if(iRet == 0){  //没有插入成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "删除成功的条数为0，删除健康建议模板失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "删除健康建议模板成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "删除健康建议模板卡失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
 	
 	
 	
 	/**
   	 * 新建健康建议
   	 * @param mbHealthPlanAdvice
   	 * @return
   	 */
 	@Transactional
 	public Map<String, Object> addMbHealthAdvice(MbHealthPlanAdvice mbHealthPlanAdvice) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			Integer iRet = mbHealthPlanAdviceMapper.insertAdvice(mbHealthPlanAdvice);		
 			if(iRet == 0){  //没有插入成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "插入成功的条数为0，新建健康建议失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "新建健康建议成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "新建健康建议失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
 	
 	/**
 	 * 查看健康建议
 	 * @param hosnum
 	 * @return
 	 */
 	public List<MbHealthPlanAdvice> getAdviceListByCondition(String hosnum,String bkbh,String headerTitle){
 		return mbHealthPlanAdviceMapper.getListByCondition(hosnum,bkbh,headerTitle);
 	}
 	
 	
 	/**
   	 * 修改健康建议模板
   	 * @param mbHealthPlanModel
   	 * @return
   	 */
 	@Transactional
 	public Map<String, Object> updateMbHealthAdviceModel(MbHealthPlanModel mbHealthPlanModel) {
 		Map<String,Object> resultMap = new HashMap<String,Object>();
 		try{
 			Integer iRet = bHealthPlanModelMapper.update(mbHealthPlanModel);		
 			if(iRet == 0){  //没有插入成功的数据
 				resultMap.put("success", "0");
 				resultMap.put("message", "修改成功的条数为0，修改健康建议模板失败！");
 				return resultMap;
 			}else{
 				resultMap.put("success", "1");
 				resultMap.put("message", "修改健康建议模板成功！");
 				return resultMap;
 			}
 		}catch(Exception e){
 			resultMap.put("success", "0");
 			resultMap.put("message", "修改健康建议模板卡失败！异常：" +e.getMessage());
 			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
 		}
 		return resultMap;
 	}
   
}
